<cfcomponent output="false" hint="I'm a FileManager Validator." extends="fileManager.util.Base">

	<cffunction name="init" access="public" output="false" returntype="Validator">
		<cfargument name="reservedDirectoryName" type="string" required="false" default="_trash" />
		<cfargument name="reservedFileName" type="string" required="false" default="" /> 
		<cfscript>
		variables.reservedDirectoryName = arguments.reservedDirectoryName;
		variables.reservedFileName = arguments.reservedFileName;
		</cfscript>
		<cfreturn this />
	</cffunction>
	
	<cffunction name="beforeFileMove" access="public" returntype="void">
		<cfscript>
		var fileName = getFileFromPath( arguments.source );
		
		if( fileExists( arguments.destination & arguments.separator & fileName ) ){
		
			$throw( message="File #fileName# exists into destination directory.", detail="Please change file name.", type="FileManager" );
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeFileUpload" access="public" returntype="void">
		<cfscript>
		
		</cfscript>
	</cffunction>
	
	<cffunction name="onFileUploaded" access="public" returntype="void">
		<cfscript>
		var filename = getFileFromPath( arguments.source );
		
		validateFileName( fileName );
		
		if( arguments.diskSpace[2].space LT arguments.diskSpace[2].space ){
			
			$throw( message="Missing Disk space.", detail="Contact site administrator for add disk space to your configuration.", type="FileManager" );
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeFileDelete" access="public" returntype="void">		<cfscript>
		var filename = getFileFromPath( arguments.source );
		
		validateFileName( fileName );
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeDirectoryRename" access="public" returntype="void">		<cfscript>
		var errorCollection = errorCollectionFactory();
		validateDirectoryName( directoryName );
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeDirectoryCreate" access="public" returntype="void">		<cfscript>
		var directoryName = getDirectoryFromPath( arguments.directoryPath );
		
		validateDirectoryName( directoryName );
		
		if( directoryExists( arguments.directoryPath ) ){
			
			$throw(  message="The name #directoryName# exists.", detail="", type="FileManager" );
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeFileList" access="public" returntype="void">		<cfscript>
		
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeDirectoryList" access="public" returntype="void">		<cfscript>
		
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeDirectoryMove" access="public" returntype="void">		<cfscript>
		var directoryName = getDirectoryFromPath( arguments.source );
		
		validateDirectoryName( directoryName );
		
		if( arguments.source eq arguments.destination ){
		
			$throw( message="Source and destination is same path.", detail="Please select an other destination path.", type="FileManager" );
		
		}
	
		if( directoryExists( arguments.destination & arguments.separator & directoryName ) ){
		
			$throw( message="Directory #directoryName# exists into destination directory.", detail="Please change directory name.", type="FileManager" );
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeFileRename" access="public" returntype="void">		<cfscript>
		var filename = getFileFromPath( arguments.source );
		
		validateFileName( fileName );
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeDirectoryDelete" access="public" returntype="void">		<cfscript>
		var directoryName = getDirectoryFromPath( arguments.source );
		
		validateDirectoryName( directoryName );
		</cfscript>
	</cffunction>

	<cffunction name="beforeFileCopy" access="public" returntype="void">		<cfscript>
		var message =  "";
		var filename = getFileFromPath( arguments.source );
		
		message = validateFileName( fileName );
		
		if( len( message ) ){
		
			errorCollection.AddError();
		
		}
			
		if( fileExists( arguments.destination & arguments.separator & fileName ) ){
		
			errorCollection.AddError();
		
			$throw( message="File name exist into your destination.", detail="Please select an other file name if you want copy.", type="FileManager" );
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="beforeDirectoryCopy" access="public" returntype="void">		<cfscript>
		var directoryName = getDirectoryFromPath( arguments.source );
		
		validateDirectoryName( directoryName );
		
		if( directoryExists( arguments.destination & variables.separator & directoryName ) ){
		
			$throw( message="Directory name exist into your destination.", detail="Please select an other directory name if you want copy.", type="FileManager" );
		
		}
		</cfscript>
	</cffunction>
	
	<!--- private --->
	
	<cffunction name="validateDirectoryName" access="private" returntype="void" >
		<cfargument name="directoryName" type="string" required="true" />
		<cfscript>
		var message = "";
		
		if( listFind( variables.reservedDirectoryName, arguments.directoryName ) ){
			
			message="The name #arguments.directoryName# is reserved.";
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="validateFileName" access="private" returntype="void" >
		<cfargument name="fileName" type="string" required="true" />
		<cfscript>
		var message = "";
		
		if( listFind( variables.reservedFileName, arguments.fileName ) ){
			
			message="The name #arguments.fileName# is reserved.";
		
		}
		</cfscript>
	</cffunction>
	
	<cffunction name="errorCollectionFactory" access="private" returntype="ErrorCollection" >
		<cfargument name="fileName" type="string" required="true" />
		<cfscript>
		return createObject( 'component', 'ErrorCollection' ).init();
		</cfscript>
	</cffunction>

</cfcomponent>